<template>
    <!-- Main content -->
    <div>
        <div class="row">
            <div class="col-xs-12">
                <theme-box title="投票活动管理">
                    <form class="form-inline" @submit.prevent v-el:form>

                    </form>
                    <table class="table table-bordered table-striped" style="width:100%" v-el:table>
                    </table>
                </theme-box>
            </div>
            <!-- /.col -->
        </div>
        <modal-container title="编辑" v-el:modal v-ref:modal class="edit-modal">
            <form @submit.prevent @reset.prevent v-el:mform>
                <div class="modal-body">
                    <table-modal :item="item" :columns-set="columnsSet"></table-modal>
                </div>

                <div class="modal-footer">
                    <v-button class="pull--left" data-dismiss="modal" @click="resetBindDrag">关闭</v-button>
                    <v-button theme="primary" type="submit">保存</v-button>
                </div>
            </form>

        </modal-container>
        <!-- /.row -->
    </div>
    <!-- /.content -->
</template>

<style lang="less" scoped>
    .form-inline .btn+.btn {
        margin-right: 10px;
    }
    .form-inline .form-group+.form-group {
        margin-left: 20px;
    }
</style>
<script>
    import {
        dateTime,
        createDataTable
    } from 'ui/collections/general.js'
    var serialize = require('form-serialize');
    import {dateFormat} from "lib/method.js";
    import {dateTransform} from "lib/method.js"

    var dataTable;
    var columnsSet;
    export default {
        data: () => ({
            item: {},
            columnsSet: [],
        }),
        methods: {
            //还原弹出框位置
            resetBindDrag() {
                this.$refs.modal._resetBindDrag();
            },
            //查询按钮
            search: function() {
                dataTable.ajax.reload();
            },
            //重置按钮
            reset: function() {
                $(".form-inline")[0].reset();
                dataTable.ajax.reload();
            }
        },
        created() {
            //datatable所用数组
            columnsSet = [{
                title: "活动id",
                data: "id",
                edit:true,
                readonly:true,
                className: "operate"
            },{
                title: "活动名称",
                data: "actName",
                edit:true,
                className: "operate"
            },{
                title: "活动名次每级奖励",
                data: "actAward",
                edit: true,
                className: "operate"
            }, {
                title: "活动奖励名次",
                data: "awardNum",
                edit: true,
                className: "operate"
            }, {
                title: "活动第一名奖励",
                data: "awardFir",
                edit: true,
                className: "operate"
            },{
                title: "状态",
                data: "state",
                className: "operate",
                render: function(data) {
                    return (data == 0) ? "正常" : "关闭"
                }
            },
//                {
//                title: "状态",
//                data: "state",
//                className: "operate",
//                render: function(data) {
//                    return (data == 0) ? "正常" : "结束"
//                }
//            },
            {
                title: "开始时间",
                data: "beginDate",
                datetime: true,
                className: "operate",
                render:function(data){
                    return dateTransform(data)
                }
            },
            {
                title: "结束时间",
                data: "endDate",
                datetime: true,
                className: "operate",
                render:function(data){
                    return dateTransform(data)
                }
            },
            {
                title: "相关操作",
                className: "operate",
                width: "160px",
                render: function(value, type, row) {
                    if(row.state == 1){
                        return `
                       <button type="button" class="btn btn-danger btn-sm state">开启</button>
                       <button type="button" class="btn btn-primary btn-sm edit">编辑</button>
                       <button type="button" class="btn btn-danger btn-sm delete">删除</button>
                      `
                    }else if(row.state == 0){
                        return `
                       <button type="button" class="btn btn-defalt btn-sm">使用中</button>
                       <button type="button" class="btn btn-danger btn-sm state">关闭</button>
                      `
                    }else{
                        return `
                       <button type="button" class="btn btn-defalt btn-sm">已结束</button>
                      `
                    }
                }
            }]
        },
        ready: async function() {
            dateTime({
                format: "yyyy-mm-dd",
                minView: "2"
            })
            this.$set("columnsSet", columnsSet)
            var self = this;
            //创建dataTable
            dataTable = $(this.$els.table).DataTable({
                "columns": this.columnsSet, //定义列
                "ajax": async function(data, callback, settings) {
                    var page = [{
                        "name": "iDisplayStart",
                        "value": 1 + data.start / data.length
                    }, {
                        "name": "iDisplayLength",
                        "value": data.length
                    }];
                    //表单序列化数据
                    var _data = serialize(self.$els.form, {
                        hash: true,
                        disabled: true,
                        empty: true
                    });
                    _data.aoData = JSON.stringify(page);
                    //发送请求
                    var result = await self.$post({
                        url: "/api/v4" + projectName_Url + "/plat/voteAct/query",
                        data:_data,
                        method: "POST"
                    })
                    if (result.respCode != "00") {
                        Midway.alert({
                            title: "操作失败",
                            content: result.content
                        })
                    }
                    callback({
                        recordsTotal: result.content.total,
                        recordsFiltered: result.content.total,
                        data: result.content.voteActList || []
                    })
                }
            });

            $(self.$el).on("click", ".edit", function() {
                dateTime({
                    format: "yyyy-mm-dd",
                    minView: "2"
                })
                var Row = $(this).closest('tr').get(0);
                const dataArray = JSON.parse(JSON.stringify(dataTable.row(Row).data()));
                self.item = dataArray;
                dateTime()
                //编辑框验证、保存、提交
                $(self.$els.mform).validate(self.item.valdate = {
                    async submitHandler() {
                        var form = self.$els.mform;
                        var _data = serialize(form, { hash: true, disabled:true, empty:true });
                        // return flase
                        _data.beginDate = _data.beginDate.replace(/-|:|\s/g, '');
                        _data.endDate = _data.endDate.replace(/-|:|\s/g, '');
                        var result = await self.$post({
                            url: "/api/v4" + projectName_Url + "/plat/voteAct/"+ _data.id,
                            data: _data,
                            method: "POST"
                        });
                        Midway.alert({title:"操作提示",content:result.errorInfo})

                        dataTable.ajax.reload(null, false);
                        $(self.$els.modal).modal('hide');
                        self.resetBindDrag()
                    },
                    rules: {
                        actName:{
                            required: true,
                            maxlength:15
                        },
                        actAward: {
                            required: true,
                            int:true,
                            max:99,
                            min:1
                        },
                        awardNum: {
                            required: true,
                            int:true,
                            max:99,
                            min:1
                        },
                        awardFir: {
                            required: true,
                            int:true,
                            max:99,
                            min:1
                        },
                        beginDate: {
                            required: true
                        },
                        endDate: {
                            required: true
                        }
                    }
                });
                $(self.$els.modal).modal('show');
            });

            $(self.$el).on("click", ".delete", async function() {
                var Row = $(this).closest('tr').get(0);
                var actName = dataTable.row(Row).data().actName;
                var id =  dataTable.row(Row).data().id;

                await Midway.confirm({
                    title: "删除活动",
                    content: "是否要删除活动:" + actName + "?"
                })
                var result = await self.$post({
                    url: "/api/v4" + projectName_Url + "/plat/voteAct/delete/"+id,
                    method: "DELETE"
                });
                dataTable.ajax.reload();
            });

            $(self.$el).on("click", ".state", async function () {
                var Row = $(this).closest('tr').get(0);
                var actName = dataTable.row(Row).data().actName;
                var id = dataTable.row(Row).data().id;
                let state = dataTable.row(Row).data().state == "0" ? '1' : '0';
                let _data = {
                    id: id,
                    state:state
                }
                if (state == "0") {
                    await Midway.confirm({
                        title: "开启活动",
                        content: "确认是否要开启:" + actName + "?"
                    })
                } else{
                    await Midway.confirm({
                        title: "关闭活动",
                        content: "确认是否要关闭:" + actName + "?"
                    })
                }
                var result = await self.$post({
                    url: "/api/v4" + projectName_Url + "/plat/voteAct/change",
                    data: _data,
                    method: "post"
                });
                Midway.alert({title:"操作提示",content:result.errorInfo});
                dataTable.ajax.reload(null, false);
                self.resetBindDrag()
            });
        },
        beforeDestroy() {
            $(this.$el).off("click")
        }
    }
</script>
